
** WEALTH INDEX **

	* Harmonize variables between endline and baseline

		* At baseline we asked "How many days in the last 7 days did you sleep hungry?"
		* At endline we asked about the past 2w. We need a variable that has the same name for the loop.

			gen slphungry_e=slphungry2wdys_e
			gen slphungry_b=slphungry7dx_b

		* At p1_b we asked "Land in Town (No Unit)"
		* At p2_b and p3_b we asked Land in Town and then Unit:

			replace landtown_b=0 if landtown_b==. & p1==1

		* ownhouse is missin at p1_b. We replace missing values in p1 baseline with 0

			replace ownhouse_b=0 if ownhouse_b==. & p1==1

	foreach p in b e {

	// Wealth Index

		gen wealth_indexstd_`p' = .
			la var wealth_indexstd_`p' "Index of wealth -- housing quality and assets (standard normal)"

		* Censor outliers (Assets)

			foreach v of varlist rooms_`p' bicycles_`p' motorcycles_`p' singlebarrelgun_`p' pistol_`p' knife_`p' cutlass_`p' mattresses_`p' generator_`p' coalpots_`p' radios_`p' tvs_`p' mats_`p' tables_`p' wheelbarrow_`p' oillantern_`p' kerosenelanterns_`p' battlanterns_`p' torch_`p' landtown_`p' farmland_`p' landdeeds_`p' watch_`p' phones_`p' shoes_`p' {
				sum `v', d
				replace `v'=r(p99) if `v'>r(p99) & `v'!=. & `v'>6
			}

			foreach v of varlist books_`p' chairs_`p' shirts_`p' trousers_`p' {
				sum `v', d
				replace `v'=r(p95) if `v'>r(p95) & `v'!=.
			}

		* Construct housing quality dummies

			gen dwellingqual_`p' = .
				la var dwellingqual_`p' "Wealth: 1 if dwelling is high quality"

			gen wallsqual_`p' = .
				la var wallsqual_`p' "Wealth: 1 if walls are high quality"

			gen floorqual_`p' = .
				la var floorqual_`p' "Wealth: 1 if floor is high quality"

			gen roofqual_`p' = .
				la var roofqual_`p' "Wealth: 1 if roof is high quality"

			gen batheaccess_`p' = .
				la var batheaccess_`p' "Wealth: 1 if has access to bathe (private or communal)"

			gen toiletqual_`p' = .
				la var toiletqual_`p' "Wealth: 1 if toilet is high quality"

			gen placedry_`p' = .
				la var placedry_`p' "Wealth: 1 if the place where they sleep is dry"


			* Place where they sleep
				* High quality == House/Hut/Shack/ Room in a house // Apartment
				* Low quality == Abandoned building, In a vehicle, Outside, Market House, Barbbing shop, Booth, Cookshop,Ghetto house, Phone booth , Video club, Video club/business, Warehouse
				replace dwellingqual_`p' = (dwelling_`p'==1 | dwelling_`p'==3) if dwelling_`p'!=.

			* Walls
				* High quality == Zinc/Metal, Cement/Stone blocks, Plywood/Reused wood, Wood planks/Shingles
				* Low quality ==Mud and sticks, Dirt block, Straw/Thatch/Mats, Cane/Palm/Trunks, Mud bricks
				replace wallsqual_`p' = (walls_`p'==1| walls_`p'==2 | walls_`p'==8 | walls_`p'==10) if walls_`p'!=.

			* Floor
				* High quality == Concrete/Stone
				* Low quality ==Red earth, Wood, Bare ground, Other
				replace floorqual_`p' = (floor_`p'==1) if floor_`p'!=.

			* Roof
				* High quality == Zinc/Metal, Concrete/Cement
				* Low quality ==  Tarpaulin/Plastic, Thatch/Palm leaf, Wood planks, Asbestos/Shingles
				replace roofqual_`p' = (roof_`p'==1 | roof_`p'==7) if roof_`p'!=.

			* Bath
				* High quality == Private bathroom, Community bathroom
				* Low quality ==River / Beach / Outside
				replace batheaccess_`p' = (bathe_`p'==1 | bathe_`p'==2) if bathe_`p'!=.

			* Toilet
				* High quality == Own flush toilet, Common flush toilet, Own latrine
				* Low quality ==Bush, Common pit latrine
				replace toiletqual_`p' = (toilet_`p' ==2 | toilet_`p' ==3| toilet_`p' ==4) if toilet_`p'!=.

			* 1 if the place they stay is dry
				replace placedry_`p' = (sleepdry_`p'==1) if sleepdry_`p'!=.


		foreach phase in 1 2 3 {
			foreach round in 1 2 3 4 5 6 7 8{

		* Replace missing values with median

			foreach v of varlist dwellingqual_`p' wallsqual_`p' floorqual_`p' roofqual_`p' batheaccess_`p' toiletqual_`p' placedry_`p' ownhouse_`p' rooms_`p' bicycles_`p' motorcycles_`p' singlebarrelgun_`p' pistol_`p' knife_`p' cutlass_`p' mattresses_`p' generator_`p' coalpots_`p' radios_`p' tvs_`p' books_`p' mats_`p' chairs_`p' tables_`p' wheelbarrow_`p' oillantern_`p' kerosenelanterns_`p' battlanterns_`p' torch_`p' landtown_`p' farmland_`p' landdeeds_`p' watch_`p' phones_`p' shoes_`p' shirts_`p' trousers_`p' eat1dx_`p' slphungry_`p' {
				cap egen median = median(`v') if p`phase'==1 & r`round'==1
				cap replace `v'=median if p`phase'==1 & r`round'==1 & `v'==.
				cap drop median
			}

		* Wealth index

			sum dwellingqual_`p' wallsqual_`p' floorqual_`p' roofqual_`p' batheaccess_`p' toiletqual_`p' placedry_`p' ownhouse_`p' rooms_`p' bicycles_`p' motorcycles_`p' singlebarrelgun_`p' pistol_`p' knife_`p'   mattresses_`p' generator_`p' coalpots_`p' radios_`p' tvs_`p' books_`p' mats_`p' chairs_`p' tables_`p' wheelbarrow_`p' oillantern_`p' kerosenelanterns_`p' battlanterns_`p' torch_`p' landtown_`p' farmland_`p' landdeeds_`p' watch_`p' phones_`p' shoes_`p' shirts_`p' trousers_`p' eat1dx_`p' slphungry_`p' if p`phase'==1 & r`round'==1
			cap pca dwellingqual_`p' wallsqual_`p' floorqual_`p' roofqual_`p' batheaccess_`p' toiletqual_`p' placedry_`p' ownhouse_`p' rooms_`p' bicycles_`p' motorcycles_`p' singlebarrelgun_`p' pistol_`p' knife_`p'   mattresses_`p' generator_`p' coalpots_`p' radios_`p' tvs_`p' books_`p' mats_`p' chairs_`p' tables_`p' wheelbarrow_`p' oillantern_`p' kerosenelanterns_`p' battlanterns_`p' torch_`p' landtown_`p' farmland_`p' landdeeds_`p' watch_`p' phones_`p' shoes_`p' shirts_`p' trousers_`p' eat1dx_`p' slphungry_`p' if p`phase'==1 & r`round'==1, factors(3)
			cap predict wealth_`p' if p`phase'==1 & r`round'==1, score

			cap summ wealth_`p' if p`phase'==1 & r`round'==1, d
			cap replace wealth_`p' = r(p99) if wealth_`p'>r(p99) & wealth_`p'!=. & p`phase'==1 & r`round'==1

			cap summ wealth_`p' if p`phase'==1 & r`round'==1, d
			cap replace wealth_`p' = r(p1) if wealth_`p'<r(p1) & wealth_`p'!=. & p`phase'==1 & r`round'==1

			cap egen w = std(wealth_`p') if p`phase'==1 & r`round'==1
			cap replace wealth_indexstd_`p' = w if p`phase'==1 & r`round'==1
			cap drop w wealth_`p'

		}
		}
		}
